﻿/**
 * Created by Eason on 2014/12/11.
 */
var mc={};
(function(){
    document.write("<script src='Tools/fastclick.js'></script>"+
        "<script src='Tools/MoCoop/settings.js'></script>"+
        "<script src='Tools/MoCoop/factory/factory.js'></script>"
    );

    mc.dealTime = function(timeStamp,timeFormat){//获取当前时间
        //转换时间格式公共方法
        Date.prototype.Format = function(fmt){
            var o = {
                "M+" : this.getMonth()+1,                 //月份
                "d+" : this.getDate(),                    //日
                "h+" : this.getHours(),                   //小时
                "m+" : this.getMinutes(),                 //分
                "s+" : this.getSeconds(),                 //秒
                "q+" : Math.floor((this.getMonth()+3)/3), //季度
                "S"  : this.getMilliseconds(),             //毫秒
                "x"  : this.getHours()<12?"上午":"下午"
            };

            if(/(y+)/.test(fmt))
                fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
            for(var k in o)
                if(new RegExp("("+ k +")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            return fmt;
        };
        if(timeStamp==undefined||timeStamp==null){
            if(timeFormat==undefined||timeStamp==null){
                return new Date().Format('yyyy-MM-dd hh:mm:ss');
            }
            else{
                return new Date().Format(timeFormat);
            }
        }else{
            if(timeFormat==undefined||timeStamp==null){
                return new Date(timeStamp).Format('yyyy-MM-dd hh:mm:ss');
            }
            else{
                return new Date(timeStamp).Format(timeFormat);
            }
        }
    };

    mc.getParamsFromURL = function(URL){//获取URL中的参数
        var paramObject = {};
        var paramStr = URL.search.substring(1);
        var params = paramStr.split('&');
        for (var i = 0; i < params.length; i++)
        {
            var pair = params[i].split('=');
            paramObject[pair[0]] = pair[1];
            paramObject[pair[0]] = decodeURI(paramObject[pair[0]])
        }
        return paramObject;
    };
    mc.hideStatusBarInAndroid = function(){//安卓平台隐藏状态栏
        if($('.mc-commonpage').hasClass('mc-sysstatuspage')){
            $('.mc-commonpage').removeClass('mc-sysstatuspage').addClass('mc-simplepage');
        }else if($('.mc-commonpage').hasClass('mc-sysstatuspage')==false&&$('.mc-commonpage').hasClass('mc-navpage')==false&&
            $('.mc-commonpage').hasClass('mc-simplepage')==false){
            $('.mc-commonpage').addClass('mc-navpage');
        }
    };
    vars.inTheAlert = false;
    mc.alert = function(message,callback){
        if(typeof message!="string"){
            message=JSON.stringify(message);
        }
        if(vars.inTheAlert == false){
            vars.inTheAlert = true;
            switch (mc.device) {
                case iOS:
                    navigator.notification.alert(
                        '',  // message
                        function(){
                            vars.inTheAlert = false;
                            if(callback!=undefined&&callback!=null&&callback!=""){
                                callback()
                            }
                        },         // callback
                        message,            // title
                        '确认'                  // buttonName
                    );
                    break;
                case Android:
                    navigator.notification.alert(
                        message,  // message
                        function(){
                            vars.inTheAlert = false;
                            if(callback!=undefined&&callback!=null&&callback!=""){
                                callback()
                            }
                        },         // callback
                        "提示",            // title
                        '确认'                  // buttonName
                    );
                    break;
                case Browser:
                    alert(message);
                    vars.inTheAlert = false;
                    break;
            }
        }
    };
    vars.inTheConfirm = false;
    mc.confirm = function(title,callback,message){
        var confirm = function(buttonIndex) {
            vars.inTheConfirm = false;
            if(buttonIndex=='2'){
                callback()

            }else if(buttonIndex=='1'){
                return false
            }
        };
        if(vars.inTheConfirm == false){
            vars.inTheConfirm = true;
            switch (mc.device) {
                case iOS:
                    var notice = "";
                    if(message!=undefined){
                        notice = message
                    }
                    navigator.notification.confirm(
                        notice, // message
                        confirm,            // callback to invoke with index of button pressed
                        title,           // title
                        ['取消','确认']     // buttonLabels
                    );
                    break;
                case Android:
                    navigator.notification.confirm(
                        title, // message
                        confirm,            // callback to invoke with index of button pressed
                        "提示",           // title
                        ['取消','确认']     // buttonLabels
                    );
                    break;
                case Browser:
                    if (!window.confirm(title)) {
                        return
                    }
                    callback();
                    break;
            }
        }

    };
    mc.selectConfirm = function(title,callback,buttonLabels,message){
        function confirm(buttonIndex) {
            var labelsIndex=parseInt(buttonIndex)-1;
            callback(buttonLabels[labelsIndex])
        }
        switch (mc.device) {
            case iOS:
                var notice = "";
                if(message!=undefined){
                    notice = message
                }
                navigator.notification.confirm(
                    notice, // message
                    confirm,            // callback to invoke with index of button pressed
                    title,           // title
                    buttonLabels     // buttonLabels
                );
                break;
            case Android:
                navigator.notification.confirm(
                    title, // message
                    confirm,            // callback to invoke with index of button pressed
                    "提示",           // title
                    buttonLabels     // buttonLabels
                );
                break;
        }

    };
    mc.tabtoswitch = function(item){
        var targetBtn;
        if(item == 0){
            targetBtn = $("#tableft");
            if(targetBtn.hasClass("pannel-tab-active") == false){
                targetBtn.addClass("pannel-tab-active");
                $("#tabright").removeClass("pannel-tab-active");
                $(".pannel-tab-line").addClass("slidetoleft").removeClass("slidetoright");
                $(".page-tabcontent-left").css("z-index","999").show();
                $(".page-tabcontent-right").css("z-index","444").hide();
            }
        }else if(item == 1){
            targetBtn = $("#tabright");
            if(targetBtn.hasClass("pannel-tab-active") == false){
                targetBtn.addClass("pannel-tab-active");
                $("#tableft").removeClass("pannel-tab-active");
                $(".pannel-tab-line").addClass("slidetoright").removeClass("slidetoleft");
                $(".page-tabcontent-right").css("z-index","999").show();
                $(".page-tabcontent-left").css("z-index","444").hide();

            }
        }
    };
})();

